﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract.Formation;
using KerriganDataContract;
using KerriganDataContract.DrivingMode;
using System.ServiceModel;

namespace KerriganService.DAC
{
    public class FormationDAC
    {
        public static List<FormationDC> GetListFormations()
        {
            List<FormationDC> list = new List<FormationDC>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = FormationDAC.CopyEntitiesToBos(db.TFormations.ToList());
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }



        #region Entity/Bo method

        public static FormationDC CopyEntityToBo(TFormation entity)
        {
            FormationDC bo = new FormationDC();

            try
            {
                bo.FormationCategory = new FormationCategoryDC();
                bo.FormationCategory = FormationCategoryDAC.CopyEntityToBo(entity.TFormationCategory);
                bo.DrivingMode = new DrivingModeDC();
                bo.DrivingMode = DrivingModeDAC.CopyEntityToBo(entity.TDrivingMode);
                bo.Duration = entity.Duration;
                bo.Id = entity.IdFormation;
                bo.Name = entity.Name;
                bo.Price = entity.Price;
                bo.UnitDuration = entity.UnitDuration;
                bo.TypeFormation = entity.TypeFormation;
                bo.IsCircuit = entity.IsCircuit;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return bo;
        }

        public static TFormation CopyBoToEntity(FormationDC bo)
        {
            TFormation entity = new TFormation();

            try
            {
                entity.TFormationCategory = new TFormationCategory();
                entity.TFormationCategory = FormationCategoryDAC.CopyBoToEntity(bo.FormationCategory);
                entity.Duration = bo.Duration;
                entity.Name = bo.Name;
                entity.Price = bo.Price;
                entity.TDrivingMode = new TDrivingMode();
                entity.TDrivingMode = DrivingModeDAC.CopyBoToEntity(bo.DrivingMode);
                entity.UnitDuration = bo.UnitDuration;
                entity.TypeFormation = bo.TypeFormation;
                entity.IsCircuit = bo.IsCircuit;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return entity;
        }

        public static List<FormationDC> CopyEntitiesToBos(List<TFormation> entities)
        {
            List<FormationDC> list = new List<FormationDC>();

            try
            {
                foreach (TFormation entity in entities)
                {
                    FormationDC dc = new FormationDC();
                    dc = FormationDAC.CopyEntityToBo(entity);
                    list.Add(dc);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        #endregion
    }
}